/*
 * @author: Spring
 * @create: 2021-06-25 10:14 AM
 * @license: MIT
 * @lastAuthor: Spring
 * @lastEditTime: 2021-06-29 10:50 AM
 * @desc:
 */
import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";
import Detail from "../views/Detail.vue";
import Setpage from "../views/Setpage.vue";
import store from "@/store/index.js";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "Home",
    component: Home,
  },
  {
    path: "/Home",
    name: "Home",
    component: Home,
  },
  {
    path: "/Detail",
    name: "Detail",
    component: Detail,
  },
  {
    path: "/Setpage",
    name: "Setpage",
    component: Setpage,
  },
  // {
  //   path: "/about",
  //   name: "About",
  //   component: () => import("../views/About.vue"),
  //   meta: { need: true },
  // },
  {
    path: "/boillingpoint",
    name: "BoilingPoint",
    component: () => import("../views/BoilingPoint.vue"),
  },
  {
    path: "/write",
    name: "Write",
    component: () => import("../views/Write.vue"),
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to, from, next) => {
  // console.log(to)
  // console.log(to.matched)
  if (to.matched.some((record) => record.meta.need)) {
    let token = store.state.token;
    if (token === null || token === "") {
      next("/login");
    } else {
      next();
    }
  } else {
    next(); // 确保一定要调用 next()
  }
});

export default router;
